
package csm.sql.pool;

// 
//ifxjdbc.jar -- *
//ifxjdbcx.jar  -- *
//ifxlang.jar
//ifxsupp.jar
//ifxsqlj.jar
//ifxtools.jar

import java.sql.Connection;
import java.sql.SQLException;

import com.informix.jdbcx.IfxConnectionPoolDataSource;

import csm.common.TypeConv;
import csm.sql.ConnectionFactory;
import csm.sql.DBConfig;
import csm.sql.SmartConnection;

/**
 * 
 * @author seungmun
 * @since 2006.1.18.
 * @version
 * @see
 */
public class InformixConnectionPool extends ConnectionFactory
{
	IfxConnectionPoolDataSource cpds = null;


	@Override
	public void createConnectionPool() throws SQLException
	{
		DBConfig dbc = DBConfig.getDBConfig(this.connectionName);
		
		cpds = new IfxConnectionPoolDataSource();
		try
		{
			Class.forName("com.informix.jdbc.IfxDriver");
		}
		catch (ClassNotFoundException e)
		{
			throw new SQLException(e.getMessage(), e);
		}

		cpds.setDataSourceName("Informix");
		cpds.setServerName(dbc.getSettings("connection.server"));
		cpds.setPortNumber(TypeConv.ToInt(dbc.getSettings("connection.port")));
		cpds.setDatabaseName(dbc.getSettings("connection.database"));

		cpds.setIfxCPMMaxPoolSize(TypeConv.ToInt(dbc.getSettings("connection.maxPoolSize")));
		cpds.setIfxCPMMinPoolSize(TypeConv.ToInt(dbc.getSettings("connection.minPoolSize")));
		cpds.setIfxCPMMaxConnections(TypeConv.ToInt(dbc.getSettings("connection.maxConnection")));

		cpds.setIfxIFX_LOCK_MODE_WAIT(60);

		cpds.setIfxIFXHOST(dbc.getSettings("connection.ip"));
		cpds.setIfxCLIENT_LOCALE(dbc.getSettings("connection.clientLocale"));
		cpds.setIfxDB_LOCALE(dbc.getSettings("connection.databaseLocale"));
		cpds.setUser(dbc.getSettings("connection.user"));
		cpds.setPassword(dbc.getSettings("connection.password"));
	}
	
	@Override
	public Connection getConnection() throws SQLException
	{
		if (cpds == null)
			createConnectionPool();

		Connection cn = cpds.getPooledConnection().getConnection();
		return new SmartConnection(cn);
	}

	@Override
	public void closeConnectionPool() throws SQLException
	{
		if (cpds == null) return;
		cpds = null;
	}
}